/*
 * Copyright 2006 Abdulla G. Abdurakhmanov (abdulla.abdurakhmanov@gmail.com).
 * 
 * Licensed under the GPL, Version 2 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.gnu.org/copyleft/gpl.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * With any your questions welcome to my e-mail 
 * or blog at http://abdulla-a.blogspot.com.
 */

package org.ofsm.creator;

import java.awt.Dimension;
import java.awt.Frame;

import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.ofsm.impl.Event;
import org.ofsm.impl.FileFiniteStateMachineBaseModel;
import org.ofsm.impl.FileFiniteStateMachineModel;
import org.ofsm.impl.StateTransition;

public class TransitionEditorDialog extends JDialog {
    private JLabel lblFromState = new JLabel();
    private JLabel lblToState = new JLabel();
    private JLabel lblWhenEvent = new JLabel();
    private JComboBox cbFromState = new JComboBox();
    private JComboBox cbToState = new JComboBox();
    private JComboBox cbWhenEvent = new JComboBox();
    private JButton btnCancel = new JButton();
    private JButton btnOk = new JButton();
    private FileFiniteStateMachineModel model;

    private TransitionEditorDialog() {
        this(null, null, "", false);
    }

    public TransitionEditorDialog(FileFiniteStateMachineModel model, Frame parent, String title, boolean modal) {        
        super(parent, title, modal);        
        this.model = model;
        try {
            jbInit();
            jbInitEx();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(443, 188));
        this.getContentPane().setLayout(null);
        this.setTitle("Add transition...");
        lblFromState.setText("From state:");
        lblFromState.setBounds(new Rectangle(15, 15, 90, 15));
        lblToState.setText("To state:");
        lblToState.setBounds(new Rectangle(15, 45, 95, 15));
        lblWhenEvent.setText("When event:");
        lblWhenEvent.setBounds(new Rectangle(15, 75, 95, 15));
        cbFromState.setBounds(new Rectangle(110, 15, 310, 20));
        cbToState.setBounds(new Rectangle(110, 45, 310, 20));
        cbWhenEvent.setBounds(new Rectangle(110, 75, 310, 20));
        btnCancel.setText("Cancel");
        btnCancel.setBounds(new Rectangle(235, 115, 80, 25));
        btnCancel.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        btnCancel_actionPerformed(e);
                    }
                });
        btnOk.setText("Ok");
        btnOk.setEnabled(false);
        btnOk.setBounds(new Rectangle(135, 115, 80, 25));
        btnOk.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        btnOk_actionPerformed(e);
                    }
                });
        this.getContentPane().add(btnOk, null);
        this.getContentPane().add(btnCancel,
                                  null);
        this.getContentPane().add(cbWhenEvent,
                                  null);
        this.getContentPane().add(cbToState,
                                  null);
        this.getContentPane().add(cbFromState,
                                  null);
        this.getContentPane().add(lblWhenEvent, null);
        this.getContentPane().add(lblToState, null);
        this.getContentPane().add(lblFromState, null);
    }

    private void btnCancel_actionPerformed(ActionEvent e) {
        setApproved(false);
        this.setVisible(false);        
    }
    
    private void btnOk_actionPerformed(ActionEvent e) {
        setApproved(true);
        StateTransition trans = new StateTransition();
        trans.setFromState(  ((FileFiniteStateMachineModel.StateInfo)cbFromState.getSelectedItem()).getState() );
        trans.setToState(  ((FileFiniteStateMachineModel.StateInfo)cbToState.getSelectedItem()).getState() );
        trans.setEvent(  ((FileFiniteStateMachineModel.EventInfo)cbWhenEvent.getSelectedItem()).getEvent() );
        model.addTransitionInfo(trans);        
        this.setVisible(false);
    }
        
    public boolean isApproved() {
        return approved;
    }

    protected void setApproved(boolean approved) {
        this.approved = approved;
    }
    
    
    protected boolean approved = false;
    
    public void selectFromState(FileFiniteStateMachineModel.StateInfo info) {
        cbFromState.setSelectedItem(info);
    }

    private void jbInitEx() {
        this.setResizable(false);    
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        this.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );
    
        for(FileFiniteStateMachineModel.StateInfo item: model.getStateInfos()) {
            cbFromState.addItem(item);
            cbToState.addItem(item);
        }
        
        FileFiniteStateMachineBaseModel.EventInfo eventInfo = model.new EventInfo();
                    Event event = new Event();
                    event.setName("newevent");
                    eventInfo.setEvent(event);

        if(model.getEventInfo(eventInfo.getEvent().getName())==null){
             try{
               model.addEventInfo(eventInfo);
               }
                catch(Exception ex) {
                    JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                }
        }
        for(FileFiniteStateMachineModel.EventInfo item: model.getEventInfos()) {
            cbWhenEvent.addItem(item);
        }
        checkValidity();
    }
    
    private boolean checkValidity() {
        if(cbFromState.getSelectedItem()!=null && cbToState.getSelectedItem()!=null && cbWhenEvent.getSelectedItem()!=null) {
            btnOk.setEnabled(true);
            return true;
        }
        else {
            btnOk.setEnabled(false);
            return false;
        }
    }

    public void selectToState(FileFiniteStateMachineBaseModel.StateInfo info) {
        cbToState.setSelectedItem(info);
    }
}
